package com.smart.home.command;

import com.smart.home.button.WireButton;
import com.smart.home.command.common.BaseCommand;
import com.smart.home.enums.SwitchEnum;
import com.smart.home.enums.WireButtonEnum;
import com.smart.home.netty.NettyUtils;
import com.smart.home.netty.handler.AbstractChannelInboundHandler;
import com.smart.home.netty.handler.WireInputButtonNameCommandHandler;
import com.smart.home.netty.handler.WireInputCommandHandler;
import com.smart.home.netty.handler.WireOutputCommandHandler;
import com.smart.home.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 有线基础控制
 *
 * @author zhangpeng
 * @version 1.0
 * @date 2017-03-09
 */
public class WireCommand extends BaseCommand {

    public WireCommand(String deviceCode, String host, int port) {
        super(deviceCode, host, port);
    }

    /**
     * 执行按钮输入状态操作
     *
     * @param wireButton 按钮
     * @return true-成功，false-失败
     */
    public boolean executeInput(WireButton wireButton) throws InterruptedException {
        if (wireButton == null || wireButton.getWireButtonEnum() == null) {
            return false;
        }
        String command = getDeviceCode() + wireButton.getWireButtonEnum().getInputExeCode();
        AbstractChannelInboundHandler handler = new WireInputCommandHandler(command);
        handler = NettyUtils.sendCommand(handler, getHost(), getPort());
        return handler.isStatus();
    }

    /**
     * 获取输入状态按钮名称(主机中存储的名称)
     *
     * @param wireButton 按钮
     * @return 按钮名称
     */
    public String inputButtonName(WireButton wireButton) {
        if (wireButton == null || wireButton.getWireButtonEnum() == null) {
            return null;
        }
        String command = getDeviceCode() + wireButton.getWireButtonEnum().getSearchInputButtonNameCode();
        WireInputButtonNameCommandHandler handler = new WireInputButtonNameCommandHandler(command);
        handler = (WireInputButtonNameCommandHandler) NettyUtils.sendCommand(handler, getHost(), getPort());
        return handler.getButtonName();
    }

    /**
     * 获取所有输入状态按钮名称
     *
     * @return key-按钮编码，value-按钮名称
     */
    public Map<String, String> inputButtonNameAll() {
        List<AbstractChannelInboundHandler> params = new ArrayList<>();
        WireButtonEnum[] buttons = WireButtonEnum.values();
        for (WireButtonEnum button : buttons) {
            String command = getDeviceCode() + button.getSearchInputButtonNameCode();
            WireInputButtonNameCommandHandler wireInputButtonNameCommandHandler = new WireInputButtonNameCommandHandler(command);
            wireInputButtonNameCommandHandler.setObj(button);
            params.add(wireInputButtonNameCommandHandler);
        }
        List<AbstractChannelInboundHandler> channelInboundHandlers = NettyUtils.sendCommand(params, getHost(), getPort());
        Map<String, String> result = new HashMap<>();
        if (CollectionUtils.isNotEmpty(channelInboundHandlers)) {
            for (AbstractChannelInboundHandler handler : channelInboundHandlers) {
                WireButtonEnum button = (WireButtonEnum) handler.getObj();
                result.put(button.getButtonCode(), handler.getButtonName());
            }
        }
        return result;
    }

    /**
     * 输出控制
     *
     * @param button 按钮
     * @param type   状态
     * @return
     */
    public boolean executeOutput(WireButton button, SwitchEnum type) {
        if (button == null || button.getWireButtonEnum() == null || type == null) {
            return false;
        }
        String command = getDeviceCode() + button.getWireButtonEnum().getOutputExeCode(type);
        WireOutputCommandHandler wireOutputCommandHandler = new WireOutputCommandHandler(command);
        AbstractChannelInboundHandler channelInboundHandler = NettyUtils.sendCommand(wireOutputCommandHandler, getHost(), getPort());
        return channelInboundHandler.isStatus();
    }

    /**
     * 获取输出按钮名称(主机中存储的名称)
     *
     * @param button 按钮
     * @return 按钮名称
     */
    public String outputButtonName(WireButton button) {
        if (button == null || button.getWireButtonEnum() == null) {
            return null;
        }
        String command = getDeviceCode() + button.getWireButtonEnum().getSearchOutputButtonNameCode();
        WireInputButtonNameCommandHandler handler = new WireInputButtonNameCommandHandler(command);
        handler = (WireInputButtonNameCommandHandler) NettyUtils.sendCommand(handler, getHost(), getPort());
        return handler.getButtonName();
    }

}
